﻿#include "netcon_plugin.h"
#include "mynetcon.h"
#include "clientmanager.h"
#include "newconrect.h"
#include "tcpmanager.h"
#include <qqml.h>

void registerTypes()
{
    //qmlRegisterType<NewConRect>("NewConRect", 1, 0, "NewConRect");

    qmlRegisterSingletonInstance("ClientManager", 1, 0, "ClientManager", ClientManager::instance());
    qmlRegisterSingletonInstance("NewConRect", 1, 0, "NewConRect", NewConRect::instance());
}

void NetConPlugin::registerTypes(const char *uri)
{
    // @uri MyNetCon
    qmlRegisterType<MyNetCon>(uri, 1, 0, "MyNetCon");  
    ::registerTypes();
}

static PluginInfo  *info;

void releasePluginInfo()
{
    delete info->m_subWin;
    delete info->m_chiBtn;
    delete info;
}

PluginInfo *getPluginInfo(QQmlApplicationEngine* engine)
{
    ::registerTypes();
    QQmlComponent* subWin = new QQmlComponent(engine, QUrl("qrc:/xuanruan/XrEngine.qml"));
    QQmlComponent* chiBtn = new QQmlComponent(engine, QUrl("qrc:/xuanruan/XrButton.qml"));
    QObject* objSubWin = subWin->create();
    QObject* objChiBtn = chiBtn->create();
    if (objSubWin && objChiBtn)
    {
        info = new PluginInfo{
                .m_name = "连接页面",
                .m_subWin = dynamic_cast<QQmlComponent*>(objSubWin),
                .m_chiBtn = dynamic_cast<QQmlComponent*>(objChiBtn)
            };
    }
    else
    {
        if (subWin->isError())
            qCritical() << subWin->errorString().toStdString().data();
        if (chiBtn->isError())
            qCritical() << chiBtn->errorString().toStdString().data();
        info = nullptr;
    }
    return info;
}

